package com.matrix.admin.service.impl;

import com.matrix.admin.service.AdminService;
import com.matrix.api.merchant.MerchantApi;
import com.matrix.api.merchant.dto.MerchantReviewDTO;
import com.matrix.api.merchant.vo.MerchantApplicationVO;
import com.matrix.common.domain.PageResult;
import com.matrix.common.domain.Result;
import com.matrix.common.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.stereotype.Service;

/**
 * @author 有点甜
 * @since 2025/7/26
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {

    @DubboReference(check = false)
    private MerchantApi merchantApi;

    @Override
    public Result<PageResult<MerchantApplicationVO>> getMerchantApplications(int pageNum, int pageSize) {
        try {
            PageResult<MerchantApplicationVO> pageResult = merchantApi.getApplicationsByPage(pageNum, pageSize);
            return Result.success(pageResult);
        } catch (RpcException e) {
            throw new BusinessException("商户服务调用失败");
        }
    }

    @Override
    public void reviewApplication(MerchantReviewDTO reviewDTO) {
        try {
            merchantApi.reviewApplication(reviewDTO);
        } catch (BusinessException e) {
            throw e;
        } catch (RpcException e) {
            throw new BusinessException("审核失败,商户服务可能暂不可用");
        }
    }
}
